using System;
using System.Collections.Generic;
using AutoChess.ConfigData;
using UnityEngine;

namespace AutoChess
{
    enum ActorDisplayShowType
    {
        NORMAL,
        UI,
        PLOT,
    }
    class ActorDisplayCmpt:ActorCmpt
    {
        private ModelConfig m_modelConfig = null;
        private CellModelLoadHelper m_cellModelLoader;
        private Animator m_modelAnimator;
        private bool m_modelCreated;
        
        protected GoDummyPoint m_actorDummyPoint = new GoDummyPoint();
        public GoDummyPoint ActorDummy
        {
            get { return m_actorDummyPoint; }
        }

        public ActorDisplayShowType m_showType { get; private set; }
        public void SetShowType(ActorDisplayShowType type)
        {
            m_showType = type;
        }
        
        private Dictionary<int,GameObject> m_dicChildModels = new Dictionary<int, GameObject>();

        protected override void Awake()
        {
            m_cellModelLoader = new CellModelLoadHelper(transform,AutoChessFieldViewCtr.Ins.LoadEventManager,"town",null,false);
            AddEventListener<Vector3,Vector3,Vector3>(ActorEventType.ModelUpdateOffset,OnModelUpdateOffset);
        }

        public void RefreshModelLayer(int layer)
        {
            m_cellModelLoader.ResetModelLayer(layer);
        }

        #region static dummy
        private void SetStaticDummyPos(DummyPoint dmPoint, Vector3 pos)
        {
            var staticDummy = m_actorDummyPoint.GetDummyPoint(dmPoint);
            if (staticDummy == null)
            {
                BLogger.Error("cannot find DummyPoint:{0},ModelID {1}",dmPoint,m_modelConfig.ModelID);
                return;
            }
            staticDummy.localPosition = pos;
            staticDummy.localRotation = Quaternion.identity;
        }

        private void RefershStaticDummyPos()
        {
            if (m_modelConfig == null)
            {
                return;
            }
            m_actorDummyPoint.Refersh(OwnActor.ActorTrans.GoTrans);
            float height = m_modelConfig.CapsuleHeight;
            SetStaticDummyPos(DummyPoint.DM_S_ROOT,Vector3.zero);
            SetStaticDummyPos(DummyPoint.DM_S_BREAST,new Vector3(0,height/2));
            SetStaticDummyPos(DummyPoint.DM_S_HEAD,new Vector3(0,height));
        }
        #endregion
        
        #region 控制接口
        public bool Create(int heroId,int fashtionId,int modelResId)
        {
            BLogger.Assert(OwnActor.GetActorType() != ActorType.UIActor);
            ClearOldModel();
            
            //TODO 优化配置数据
            m_modelConfig = new ModelConfig(heroId,fashtionId, modelResId);
            if (m_modelConfig == null)
            {
                BLogger.EditorFatal("create model failed, ModelId[{0}] Actor[{1}]",modelResId,OwnActor.GetGameObjectName());
                return false;
            }

            AnimatorInterface animInterface = new UnityAnimatorInterface();
            LoadActorModel(OnModelCreate);
            return true;
        }
        
        public void ClearOldModel()
        {
            DestroyModelGo();
            m_modelCreated = false;
        }

        private void DestroyModelGo()
        {
            m_cellModelLoader.UnloadModel();
        }
        #endregion
        
        #region LoadModel
        
        private void LoadActorModel(Action loadedCall)
        {
            //TODO 这里其实也应该封装在Unity 或者base 域。 logic域里面有时候应对这种复杂多资源组合和预加载的情况应该将其封装到 base中作为基础组件
            m_cellModelLoader.SetData(m_modelConfig.HeroID,m_modelConfig.FashtionID);
            m_cellModelLoader.SetOnLoadComplete(OnModelCreate);
            m_cellModelLoader.LoadModel();
        }

        private void OnModelCreate()
        {
            ActorEventHelper.SendActorModelCreate(OwnActor);
        }

        private void OnModelUpdateOffset(Vector3 localPosition,Vector3 scale,Vector3 localAngle)
        {
            m_cellModelLoader.SetModelOffset(localPosition,scale,localAngle);
        }
        #endregion
        
        public float GetDeathTime()
        {
            return 0;
        }

        public Animator TryGetUnityAnimator()
        {
            if (m_modelAnimator != null)
            {
                return m_modelAnimator;
            }

            m_modelAnimator = m_cellModelLoader.GetAnimtor();
            return m_modelAnimator;
        }
    }
}